package pieces;

import java.util.ArrayList;

import players.Player;

import model.*;

public class King extends MoveDependent
{
	public King(Location loc, GridGame<Piece> grid, int color)
	{
		super(loc, grid, KING, color);
	}
	@Override
	public ArrayList<Location> getMoveLocations()
	{
		ArrayList<Location> locs = new ArrayList<Location>();
		locs.add(getLocationInDirection(Location.NORTH));
		locs.add(getLocationInDirection(Location.NORTHWEST));
		locs.add(getLocationInDirection(Location.NORTHEAST));
		locs.add(getLocationInDirection(Location.WEST));
		locs.add(getLocationInDirection(Location.EAST));
		locs.add(getLocationInDirection(Location.SOUTH));
		locs.add(getLocationInDirection(Location.SOUTHWEST));
		locs.add(getLocationInDirection(Location.SOUTHEAST));
		ArrayList<Location> returned = new ArrayList<Location>();
		int row = 0;
		if (color == Player.WHITE)
			row = 7;
		if (canCastleLeft())
			locs.add(new Location(row, 2));
		if (canCastleRight())
			locs.add(new Location(row, 6));
		for (Location loc:locs)
			if (loc != null && grid.inBounds(loc))
				returned.add(loc);
		return returned;
	}
	@Override
	public int getValue()
	{
		return Integer.MAX_VALUE;
	}
	private boolean canCastleLeft()
	{
		if (hasMoved())
			return false;
		int row = 0;
		if (color == Player.WHITE)
			row = 7;
		boolean leftClear = true;
		for (int x = 1; x < 4; x++)
			if (grid.getObjectAt(new Location(row, x)) != null)
				leftClear = false;
		return leftClear;
	}
	private boolean canCastleRight()
	{
		if (hasMoved())
			return false;
		int row = 0;
		if (color == Player.WHITE)
			row = 7;
		boolean rightClear = true;
		for (int x = 5; x < 7; x++)
			if (grid.getObjectAt(new Location(row, x)) != null)
				rightClear = false;
		return rightClear;
	}
}
